/*
 * @(#)FileRenderer.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */
package grupb.upf.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 * <p>This is the file renderer of the JList. It let us to modify how it
 * is going to be shown.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public class FileRenderer extends JLabel implements ListCellRenderer {

    /**
     * Type photo folder.
     */
    private static final int TYPE_FOLDER_PHOTOS = 0;
    /**
     * Type video folder.
     */
    private static final int TYPE_FOLDER_VIDEOS = 1;
    /**
     * Type unknown.
     */
    private static final int TYPE_UNKNOWN = 2;
    /**
     * Type video.
     */
    private static final int TYPE_VIDEO = 3;
    /**
     * Type image.
     */
    private static final int TYPE_IMAGE = 4;
    /**
     * The array of predefined icons.
     */
    private ImageIcon [] icons;
    /**
     * The type of the file to process.
     */
    private int type;
    /**
     * The size of the icons.
     */
    private int iconSize;

    /**
     * The constructor of the class.
     * @param size the height and width of the icons
     * @param t the type of folders that we are exploring (image or video)
     */
    public FileRenderer(final int size, final int t) {
        this.iconSize = size;
        this.type = t;
        icons = new ImageIcon[4];
        icons[TYPE_UNKNOWN] = getImageIcon("mime-empty.png");
        icons[TYPE_VIDEO] = getImageIcon("video.png");
        icons[TYPE_FOLDER_PHOTOS] = getImageIcon("folder_photos.png");
        icons[TYPE_FOLDER_VIDEOS] = getImageIcon("folder_videos.png");

    }

    /**
     * Method to get the renderer for the list.
     * @param list the JList
     * @param value the file
     * @param index the index
     * @param isSelected if isSelected or not
     * @param cellHasFocus if has focused or not
     * @return the component itself
     */
    @Override
    public Component getListCellRendererComponent(
            final JList list, final Object value, final int index,
            final boolean isSelected, final boolean cellHasFocus) {

        final File f = (File) value;

        int typeOfFile = extensionType(f);
        if (typeOfFile != TYPE_IMAGE) {
            setIcon(icons[typeOfFile]);
        } else {
            setIcon(grupb.upf.imageProcessing.Thumbnail.getThumbnail(
                    f.getAbsolutePath(), iconSize, iconSize));
        }

        setOpaque(true);

        if (isSelected) {
            setBackground(new Color(240, 240, 240, 240));
            setForeground(Color.BLACK);
        } else {
            setBackground(Color.WHITE);
            setForeground(Color.BLACK);
        }

        setText(f.getName());
        final Dimension preferredSize =
                new Dimension(iconSize + 110, iconSize);
        this.setPreferredSize(preferredSize);
        return this;
    }

    /**
     * Method to get the extension.
     * @param f the file
     * @return the extension (its code)
     */
    private int extensionType(final File f) {
        final String name = f.getName();
        String ext = "";
        if (name.indexOf(".") != -1) {
            ext = name.substring(name.lastIndexOf('.') + 1, name.length());
        }
        /* Photos */
        if (ext.equals("jpeg")) {
            return TYPE_IMAGE;
        }
        if (ext.equals("jpg")) {
            return TYPE_IMAGE;
        }
        if (ext.equals("gif")) {
            return TYPE_IMAGE;
        }
        if (ext.equals("bmp")) {
            return TYPE_IMAGE;
        }
        if (ext.equals("png")) {
            return TYPE_IMAGE;
        }
        /* Videos */
        if (ext.equals("avi")) {
            return TYPE_VIDEO;
        }
        if (ext.equals("mpg")) {
            return TYPE_VIDEO;
        }
        if (ext.equals("mpeg")) {
            return TYPE_VIDEO;
        }
        /* Folder */
        if (f.isDirectory()) {
            return type;
        }

        return TYPE_UNKNOWN;
    }

    /**
     * Method to get the icon.
     * @param fileName the filename of the icon
     * @return the icon
     */
    public ImageIcon getImageIcon(final String fileName) {
        final ImageIcon img =
                new ImageIcon(getClass().getResource("/graphics/" + fileName));
        return new ImageIcon(
                img.getImage().getScaledInstance(iconSize, iconSize,
                Image.SCALE_SMOOTH));
    }
}
